#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn=20000, maxm=500, maxq=100000, mods=998244353;

struct Ques {
    int l, r, m, id;

    static bool cmp(Ques &a, Ques &b) {return a.l<b.l;};
} que[maxq+1];

int n, q, v[maxn+1], w[maxn+1], ans[maxq+1][2];

void solve(int l, int r, int tl, int tr) {
    static int f[maxn+1][maxm+1];
    static ll g[maxn+1][maxm+1];
    for (int i=l; i<=r; i++) {
        for (int j=0; j<=maxm; j++) f[i][j] = g[i][j] = 0;
    }

    if (l==r) {
        for (int i=tl; i<=tr; i++) {
            int id=que[i].id;
            if (que[i].l==que[i].r) {
                int t=que[i].l;
                if (que[i].m>=w[t]) ans[id][0]=v[t], ans[id][1]=1;
                else ans[id][0]=ans[id][1]=0;
            }
        }
        return;
    }

    int mid=(l+r)/2;
    f[mid][0]=f[mid+1][0]=0, f[mid][w[mid]]=v[mid], f[mid+1][w[mid+1]]=v[mid+1];
    g[mid][0] = g[mid+1][0] = g[mid][w[mid]] = g[mid+1][w[mid+1]] = 1;

    for (int i=mid-1; i>=l; i--) {
        for (int j=0; j<=maxm; j++) {
            if (g[i+1][j]) {
                if (f[i+1][j]>f[i][j]) f[i][j] = f[i+1][j], g[i][j] = 0;
                if (f[i+1][j]==f[i][j]) g[i][j] = (g[i][j]+g[i+1][j])%mods;
            }
            if (j>=w[i] && g[i+1][j-w[i]]) {
                if (f[i+1][j-w[i]]+v[i]>f[i][j]) f[i][j] = f[i+1][j-w[i]]+v[i], g[i][j] = 0;
                if (f[i+1][j-w[i]]+v[i]==f[i][j]) g[i][j] = (g[i][j]+g[i+1][j-w[i]])%mods;
            }
        }
    }
    for (int i=mid+2; i<=r; i++) {
        for (int j=0; j<=maxm; j++) {
            if (g[i-1][j]) {
                if (f[i-1][j]>f[i][j]) f[i][j] = f[i-1][j], g[i][j] = 0;
                if (f[i-1][j]==f[i][j]) g[i][j] = (g[i][j]+g[i-1][j])%mods;
            }
            if (j>=w[i] && g[i-1][j-w[i]]) {
                if (f[i-1][j-w[i]]+v[i]>f[i][j]) f[i][j] = f[i-1][j-w[i]]+v[i], g[i][j] = 0;
                if (f[i-1][j-w[i]]+v[i]==f[i][j]) g[i][j] = (g[i][j]+g[i-1][j-w[i]])%mods;
            }
        }
    }
    for (int i=mid+1; i<=r; i++) {
        for (int j=1; j<=maxm; j++) {
            if (f[i][j]<f[i][j-1]) f[i][j]=f[i][j-1], g[i][j]=0;
            if (f[i][j]==f[i][j-1]) g[i][j] = (g[i][j]+g[i][j-1])%mods;
        }
    }

    int i;
    for (i=tl; i<=tr && que[i].l<=mid; i++) {
        if (que[i].r>mid && que[i].r<=r) {
            int id=que[i].id, m=que[i].m, ql=que[i].l, qr=que[i].r;
            for (int j=0; j<=m; j++) {
                if (ans[id][0]<f[ql][j]+f[qr][m-j]) ans[id][0]=f[ql][j]+f[qr][m-j], ans[id][1]=0;
                if (ans[id][0]==f[ql][j]+f[qr][m-j]) ans[id][1]=(ans[id][1]+g[ql][j]*g[qr][m-j]%mods)%mods;
            }
        }
    }
    if (tl<i) solve(l, mid, tl, i-1);
    if (i<=tr) solve(mid+1, r, i, tr);
}

int main() {
    freopen("knapsack.in", "r", stdin);
    freopen("knapsack.out", "w", stdout);

    scanf("%d", &n);
    for (int i=1; i<=n; i++) scanf("%d %d", v+i, w+i);
    scanf("%d", &q);
    for (int i=1; i<=q; i++) {
        scanf("%d %d %d", &que[i].l, &que[i].r, &que[i].m);
        que[i].id = i;
    }
    sort(que+1, que+q+1, Ques::cmp);

    solve(1, n, 1, q);
    for (int i=1; i<=q; i++) {
        printf("%d %d\n", (ans[i][0]!=0)*ans[i][0], (ans[i][0]!=0)*ans[i][1]);
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}
